/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT, THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package com.lineage.server.command.executor;

import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.lineage.server.datatables.ItemTable;
import com.lineage.server.model.L1Inventory;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.packetserver.S_ServerMessage;
import com.lineage.server.packetserver.S_SystemMessage;
import com.lineage.server.templates.L1Item;

/**
 * GM指令：创立道具
 */
public class L1CreateItem implements L1CommandExecutor {
    private static Logger _log = Logger.getLogger(L1CreateItem.class.getName());

    public static L1CommandExecutor getInstance() {
        return new L1CreateItem();
    }

    private L1CreateItem() {
    }

    @Override
    public void execute(final L1PcInstance pc, final String cmdName,
            final String arg) {
        try {
            final StringTokenizer st = new StringTokenizer(arg);
            final String nameid = st.nextToken();
            int count = 1;
            if (st.hasMoreTokens()) {
                count = Integer.parseInt(st.nextToken());
            }
            int enchant = 0;
            if (st.hasMoreTokens()) {
                enchant = Integer.parseInt(st.nextToken());
            }
            int isId = 0;
            if (st.hasMoreTokens()) {
                isId = Integer.parseInt(st.nextToken());
            }
            String attr = "";
            if (st.hasMoreTokens()) {
                attr = st.nextToken();
            }
            int attLevel = 0;
            if (st.hasMoreTokens()) {
                attLevel = Integer.parseInt(st.nextToken());
            }
            int itemid = 0;
            try {
                itemid = Integer.parseInt(nameid);
            } catch (final NumberFormatException e) {
                itemid = ItemTable.getInstance().findItemIdByNameWithoutSpace(
                        nameid);
                if (itemid == 0) {
                    pc.sendPackets(new S_SystemMessage("找不到符合条件项目。"));
                    return;
                }
            }
            final L1Item temp = ItemTable.getInstance().getTemplate(itemid);
            if (temp != null) {
                if (temp.isStackable()) {
                    final L1ItemInstance item = ItemTable.getInstance()
                            .createItem(itemid);
                    item.setEnchantLevel(0);
                    item.setCount(count);
                    if (isId == 1) {
                        item.setIdentified(true);
                    }
                    if (pc.getInventory().checkAddItem(item, count) == L1Inventory.OK) {
                        pc.getInventory().storeItem(item);
                        pc.sendPackets(new S_ServerMessage(403, // 获得%0%o 。
                                item.getLogName() + "(ID:" + itemid + ")"));
                    }
                } else {
                    L1ItemInstance item = null;
                    int createCount;
                    for (createCount = 0; createCount < count; createCount++) {
                        item = ItemTable.getInstance().createItem(itemid);
                        item.setEnchantLevel(enchant);
                        if ((item.getItem().getType2() == 2)
                                && ((item.getItem().getType() >= 8) && (item
                                        .getItem().getType() <= 12))) { // 饰品类
                            boolean award = false;
                            for (int i = 0; i < enchant; i++) {
                                if (i == 5) {
                                    award = true;
                                } else {
                                    award = false;
                                }
                                switch (item.getItem().getGrade()) {
                                    case 0: // 上等
                                        // 四属性 +1
                                        item.setFireMr(item.getFireMr() + 1);
                                        item.setWaterMr(item.getWaterMr() + 1);
                                        item.setEarthMr(item.getEarthMr() + 1);
                                        item.setWindMr(item.getWindMr() + 1);
                                        // LV6 额外奖励：体力与魔力回复量 +1
                                        if (award) {
                                            item.setHpr(item.getHpr() + 1);
                                            item.setMpr(item.getMpr() + 1);
                                        }
                                        break;
                                    case 1: // 中等
                                        // HP +2
                                        item.setaddHp(item.getaddHp() + 2);
                                        // LV6 额外奖励：魔防 +1
                                        if (award) {
                                            item.setM_Def(item.getM_Def() + 1);
                                        }
                                        break;
                                    case 2: // 初等
                                        // MP +1
                                        item.setaddMp(item.getaddMp() + 1);
                                        // LV6 额外奖励：魔攻 +1
                                        if (award) {
                                            item.setaddSp(item.getaddSp() + 1);
                                        }
                                        break;
                                    case 3: // 特等
                                        // 功能台版未实装。
                                        break;
                                    default:
                                        break;
                                }
                            }
                        } else if (item.getItem().getType2() == 1) { // 武器类
                            if (attr.equalsIgnoreCase("地")
                                    || attr.equalsIgnoreCase("1")) {
                                if ((attLevel > 0) && (attLevel <= 3)) {
                                    item.setAttrEnchantKind(1);
                                    item.setAttrEnchantLevel(attLevel);
                                }
                            } else if (attr.equalsIgnoreCase("火")
                                    || attr.equalsIgnoreCase("2")) {
                                if ((attLevel > 0) && (attLevel <= 3)) {
                                    item.setAttrEnchantKind(2);
                                    item.setAttrEnchantLevel(attLevel);
                                }
                            } else if (attr.equalsIgnoreCase("水")
                                    || attr.equalsIgnoreCase("4")) {
                                if ((attLevel > 0) && (attLevel <= 3)) {
                                    item.setAttrEnchantKind(4);
                                    item.setAttrEnchantLevel(attLevel);
                                }
                            } else if (attr.equalsIgnoreCase("风")
                                    || attr.equalsIgnoreCase("8")) {
                                if ((attLevel > 0) && (attLevel <= 3)) {
                                    item.setAttrEnchantKind(8);
                                    item.setAttrEnchantLevel(attLevel);
                                }
                            }
                        }
                        if (isId == 1) {
                            item.setIdentified(true);
                        }
                        if (pc.getInventory().checkAddItem(item, 1) == L1Inventory.OK) {
                            pc.getInventory().storeItem(item);
                        } else {
                            break;
                        }
                    }
                    if (createCount > 0) {
                        pc.sendPackets(new S_ServerMessage(403, // %0を手に入れました。
                                item.getLogName() + "(ID:" + itemid + ")"));
                    }
                }
            } else {
                pc.sendPackets(new S_SystemMessage("指定的道具编号不存在"));
            }
        } catch (final Exception e) {
            _log.log(Level.SEVERE, e.getLocalizedMessage(), e);
            pc.sendPackets(new S_SystemMessage(
                    "请输入 .item itemid|name [数量] [强化等级] [鉴定状态] [武器属性] [属性等级]。"));
        }
    }
}
